// 2025/8/21
// 体育课测验(二)

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param numProject int整型 
     * @param groups int整型vector<vector<>> 
     * @return int整型vector
     */
    vector<int> findOrder(int numProject, vector<vector<int> >& groups) {
        // write code here
        vector<int> limit(numProject, 0);
        vector<vector<int>> adjlist(numProject);
        for(auto& group : groups)
        {
            limit[group[0]]++;
            adjlist[group[1]].emplace_back(group[0]);
        }
        queue<int> ready;
        vector<int> ans;
        for(int i = 0; i < numProject; i++)
        {
            if(limit[i] == 0)
                ready.emplace(i);
        }
        while(!ready.empty())
        {
            int next = ready.front(); ready.pop();
            ans.emplace_back(next);
            for(auto num : adjlist[next])
            {
                if(--limit[num] == 0)
                    ready.emplace(num);
            }
        }
        if(ans.size() == numProject)
            return ans;
        else
            return (vector<int>){};
    }
};